
import pyhdfs,pprint,os,time
from Tools.Logs import Logs


class Hdfstools :

    def __init__(self,cluster,dir,user,min):
        self.cluster = cluster
        self.user = user
        self.min = min
        self.dir = dir 
        self.dirinfo = {}
        today=time.strftime("%Y%m%d",time.localtime())
        self.log_home_path = "/logs/"+today
        self.logfilename = self.cluster.split(":")[0] +"."+self.dir.split("/")[-1]+"."+str(self.min)
	self.sortfile = self.logfilename + ".sort"
        self.fulllogfile = "/home/hadoop/distcp/python_run/Hdp_Tools/logs/" + today+ "/" + self.sortfile
        #self.fulllogfile = "/home/hadoop/distcp/python_run/Hdp_Tools/logs/" + today+ "/" + self.logfilename
        print "self.cluster : %s \nself.user : %s\nself.min : %s\nself.logfilename : %s\nself.fulllogfile : %s\n" % (self.cluster,self.user,self.min,self.logfilename,self.fulllogfile)

    def get_dir_summary(self,dir_path):
        fs = pyhdfs.HdfsClient(hosts=self.cluster, user_name=self.user)
        for i in fs.listdir(dir_path):
            dirpath = dir_path +"/" +  i
            dirpathsize = fs.get_content_summary(dirpath).get("length")
            if dirpathsize > self.min and fs.get_file_status(dirpath).type == "DIRECTORY":
                self.get_dir_summary(dirpath)
                dir_summary = "%s\t%s\n" % (dirpath,dirpathsize)
                self.dirinfo[dirpath] = dirpathsize
                print dir_summary
                Logs(self.logfilename ).add(dir_summary)

    def sort_by_value(self):
        self.get_dir_summary(self.dir)
        pprint.pprint(self.dirinfo)
        sortfile = self.logfilename + ".sort"
        items = self.dirinfo.items()
        backitems = [[v[1], v[0]] for v in items]
        backitems.sort()
        list1 = [backitems[i][1] for i in range(0, len(backitems))]
        for i in list1:
            Logs(sortfile).add(i+"\n")

    def shell_distcp(self):
        distcp_cmd="cd /home/hadoop/distcp ; nohup sh distcp.sh %s  &" % (self.fulllogfile)
        print "cmd : %s " % distcp_cmd
        os.system(distcp_cmd)


